package com.jian.controller;

import com.jian.common.BaseResponse;
import com.jian.common.ErrorCode;
import com.jian.common.ResultUtils;
import com.jian.constant.UserContent;
import com.jian.domain.User;
import com.jian.requstBody.UserLoginRequest;
import com.jian.requstBody.UserRegisterRequest;
import com.jian.requstBody.UserUpdateRequest;
import com.jian.service.UserService;
import com.jian.utils.RedisToken;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.jian.constant.UserContent.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    // 根据单个id查询用户
    @GetMapping("{id}")
    public BaseResponse getUserById(@PathVariable("id") Long id) {
        User user = userService.getById(id);
        if (user == null) {
            return ResultUtils.Error(ErrorCode.PARAM_ERROR, "该用户不存在");
        }
        return ResultUtils.Success(user);
    }

    // 查询所有用户
    @GetMapping()
    public BaseResponse getUsers(@RequestBody String token) {
        if (token == null || "".equals(token))
            return ResultUtils.Error(ErrorCode.PARAM_ERROR, "token无效");
        if (!isAdmin(token))
            return ResultUtils.Error(ErrorCode.NO_AUTH, "用户权限不足");
        List<User> list = userService.searchUsers();
        if (list == null) {
            return ResultUtils.Error(ErrorCode.SELETE_ERROR, "用户表查询失败,或者结果为空");
        }
        return ResultUtils.Success(list);
    }

    // 用户注册
    @PostMapping("/register")
    public BaseResponse userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null)
            return ResultUtils.Error(ErrorCode.PARAM_NULL_ERROR);

        String account = userRegisterRequest.getUserAccount();
        String password = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(account, password, checkPassword))
            return ResultUtils.Error(ErrorCode.PARAM_ERROR);

        long userID = userService.userRegister(account, password, checkPassword);

        if (userID > 0) {
            return ResultUtils.Success();
        }
        return ResultUtils.Error(ErrorCode.LOGIN_FAILED);
    }

    // 用户登录
    @PostMapping("/login")
    public BaseResponse userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null)
            return ResultUtils.Error(ErrorCode.PARAM_NULL_ERROR);

        String account = userLoginRequest.getUserAccount();
        String password = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(account, password))
            return ResultUtils.Error(ErrorCode.PARAM_ERROR, "账号或者密码不符合要求");
        User user = userService.userLogin(account, password, request);
        if (user == null) {
            return ResultUtils.Error(ErrorCode.LOGIN_FAILED, "账号或者密码错误");
        }
        return ResultUtils.Success(user);
    }

    // 用户退出登录
    @PostMapping("/logout")
    public BaseResponse userLogout(HttpServletRequest request, @RequestBody User user) {
        userService.userLogout(request, user.getToken());
        return ResultUtils.Success();
    }

    // 用户修改信息
    @PostMapping("/updateInfo")
    public BaseResponse userUpdateInfo(@RequestBody UserUpdateRequest newUser) {
        if (!userService.userUpdateInfo(newUser)) {
            return ResultUtils.Error(ErrorCode.UPDATE_ERROR, "mybatis-plus更新失败了");
        }
        return ResultUtils.Success();
    }

    /**
     * 是否为管理员
     *
     * @param token 请求参数
     * @return 是否管理员
     */
    private boolean isAdmin(String token) {
        // 鉴权 判断登录的用户是否是管理员
//        User user = (User) request.getSession().getAttribute(USER_LOGIN_STATUS);
        int userRole = userService.currentUserRole(token);
        if (userRole == NOT_LOGIN)
            return false;
        return userRole == ADMINISTRATOR;
    }
}
